package server.jsons;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;

import com.google.gson.annotations.Expose;

public class Daily {

	private static Logger logger = Logger.getLogger(Daily.class.getName());

	@Expose
	private String date;
	@Expose
	private int satisfaction;
	@Expose
	private int motivation;
	@Expose
	private String sleeptime;
	@Expose
	private String waketime;
	private Date wakedate = null;
	private Date sleepdate = null;

	public Daily() {

	}

	public Date getWakedate() {

		if(this.wakedate == null){
			try {

				int hour = Integer
						.parseInt(this.getWaketime().substring(0, 2));
				int minute = Integer.parseInt(this.getWaketime()
						.substring(3, 5));
				SimpleDateFormat f = new SimpleDateFormat();
				f.applyPattern("yyyy-MM-dd HH:mm:ss");
				Calendar c = new GregorianCalendar();
				c.setTime(f.parse(this.date));
				c.set(Calendar.HOUR, hour);
				c.set(Calendar.MINUTE, minute);
				this.wakedate = c.getTime();

			} catch (ParseException e) {
				logger.warn(e);
			}
		}
		return wakedate;
	}

	public void setWakedate(Date wakedate) {
		
		this.wakedate = wakedate;
	}

	public Date getSleepdate() {
		if (this.sleepdate == null) {
			try {

				int hour = Integer
						.parseInt(this.getSleeptime().substring(0, 2));
				int minute = Integer.parseInt(this.getSleeptime()
						.substring(3, 5));
				SimpleDateFormat f = new SimpleDateFormat();
				f.applyPattern("yyyy-MM-dd HH:mm:ss");
				Calendar c = new GregorianCalendar();
				c.setTime(f.parse(this.date));
				c.set(Calendar.HOUR, hour);
				c.set(Calendar.MINUTE, minute);

				if (18 < hour && hour < 24) {
					logger.debug("Same Day for Sleepdate");
					this.sleepdate = c.getTime();
				} else {
					c.add(Calendar.DAY_OF_MONTH, 1);
					logger.debug("Date of Tomorrow for Sleepdate");
					this.sleepdate = c.getTime();
				}
			} catch (ParseException e) {
				logger.warn(e);
			}
		}
		return sleepdate;
	}

	public void setSleepdate(Date sleepdate) {
		this.sleepdate = sleepdate;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public void setSleeptime(String sleeptime) {
		this.sleeptime = sleeptime;
	}

	public void setWaketime(String waketime) {
		this.waketime = waketime;
	}

	public int getSatisfaction() {
		return satisfaction;
	}

	public void setSatisfaction(int satisfaction) {
		this.satisfaction = satisfaction;
	}

	public int getMotivation() {
		return motivation;
	}

	public void setMotivation(int motivation) {
		this.motivation = motivation;
	}

	public String getSleeptime() {
		return sleeptime;
	}

	public String getWaketime() {
		return waketime;
	}

}
